﻿Public Class frmGiaoVien

    Sub LoadDanhSach()
        Dim _dt As DataTable
        _dt = GiaoVienDAO.SelectAll()
        dgvTTGiaoVien.DataSource = _dt
    End Sub

    Sub Display()
        If (dgvTTGiaoVien.SelectedRows.Count > 0) Then
            txtMAGV.Text = dgvTTGiaoVien.SelectedRows(0).Cells(0).Value.ToString()
            txtHOTENGV.Text = dgvTTGiaoVien.SelectedRows(0).Cells(1).Value.ToString()
            dtpNGAYSINH.Value = dgvTTGiaoVien.SelectedRows(0).Cells(2).Value
            cboGIOITINH.Text = dgvTTGiaoVien.SelectedRows(0).Cells(3).Value.ToString()
            txtDIACHI.Text = dgvTTGiaoVien.SelectedRows(0).Cells(4).Value.ToString()
            txtSDTm.Text = dgvTTGiaoVien.SelectedRows(0).Cells(5).Value.ToString()
            cboCHUYENMON.SelectedValue = dgvTTGiaoVien.SelectedRows(0).Cells(6).Value.ToString()
        End If
    End Sub

    Private Sub btnTHEM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTHEM.Click

        Dim _gv As GiaoVienDTO = New GiaoVienDTO()
        _gv.HoTen = txtHOTENGV.Text
        _gv.NgaySinh = dtpNGAYSINH.Text
        _gv.GioiTinh = cboGIOITINH.Text
        _gv.DiaChi = txtDIACHI.Text
        _gv.DienThoai = txtSDTm.Text
        _gv.MaChuyenMon = cboCHUYENMON.SelectedValue
        Try
            GiaoVienDAO.Insert(_gv)
            MessageBox.Show("Thêm giáo viên thành công.")
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
        LoadDanhSach()
    End Sub

    Private Sub frmGiaoVien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim _dt As New DataTable
        _dt = ChuyenMonDAO.SelectAll()
        cboCHUYENMON.DataSource = _dt
        cboCHUYENMON.DisplayMember = "TenChuyenMon"
        cboCHUYENMON.ValueMember = "MaChuyenMon"

        cboGIOITINH.SelectedIndex = 0
        LoadDanhSach()
    End Sub


    Private Sub btnSUA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSUA.Click
        If (dgvTTGiaoVien.SelectedRows.Count > 0) Then
            Dim _gv As GiaoVienDTO = New GiaoVienDTO()
            _gv.MaGV = txtMAGV.Text
            _gv.HoTen = txtHOTENGV.Text
            _gv.NgaySinh = dtpNGAYSINH.Text
            _gv.GioiTinh = cboGIOITINH.Text
            _gv.DiaChi = txtDIACHI.Text
            _gv.DienThoai = txtSDTm.Text
            _gv.MaChuyenMon = cboCHUYENMON.SelectedValue
            Try
                GiaoVienDAO.Update(_gv)
                MessageBox.Show("Cập nhật thành công.")
            Catch ex As Exception
                MessageBox.Show(ex.ToString())
            End Try
            LoadDanhSach()
        End If
    End Sub

    Private Sub btnXOA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXOA.Click
        Dim _maGV As GiaoVienDTO = New GiaoVienDTO()
        _maGV.MaGV = txtMAGV.Text
        Try
            GiaoVienDAO.Delete(_maGV)
            MessageBox.Show("Xóa giáo viên thành công.")
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
        LoadDanhSach()
    End Sub

    Private Sub dgvTTGiaoVien_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvTTGiaoVien.CellContentClick
        Display()
    End Sub

    Private Sub dgvTTGiaoVien_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvTTGiaoVien.SelectionChanged
        Display()
    End Sub

    Private Sub btnTHOAT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTHOAT.Click
        Me.Close()
    End Sub

    Private Sub frmGiaoVien_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If (dgvTTGiaoVien.SelectedRows.Count > 0) Then
            If MessageBox.Show("Bạn có muốn lưu lại những thay đổi", "Thông Báo", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                Dim _gv As GiaoVienDTO = New GiaoVienDTO()
                _gv.MaGV = txtMAGV.Text
                _gv.HoTen = txtHOTENGV.Text
                _gv.NgaySinh = dtpNGAYSINH.Text
                _gv.GioiTinh = cboGIOITINH.Text
                _gv.DiaChi = txtDIACHI.Text
                _gv.DienThoai = txtSDTm.Text
                _gv.MaChuyenMon = cboCHUYENMON.SelectedValue
                Try
                    GiaoVienDAO.Update(_gv)
                Catch ex As Exception
                    MessageBox.Show(ex.ToString())
                End Try
            End If
        End If
    End Sub
    
End Class